﻿/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.xmlutils {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * Dispatched when configuration file is loaded successfully.
	 * @eventType com.puny.utils.XMLReader.COMPLETE
	 */
	[Event(name="completed", type="com.puny.utils.XMLReader")]

	/**
	 * $Revision: 837 $ <br/>
	 */	
	public class XMLReader extends EventDispatcher {
		private var _url:String;
		private var _loader:URLLoader;
		private static var _xmlDocs:Object = new Object();
		private var _xmlDoc:XML = null;
		public static const COMPLETED:String = "xml completed";
		
		/**
		 * Set URL of configuration and read it.
		 */
		public function setURL(url:String):void {
			_loader = new URLLoader();
			
			_loader.addEventListener(ProgressEvent.PROGRESS,progressHandler);
			_loader.addEventListener(Event.COMPLETE,completeHandler);
			_loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
			_loader.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
			
			_url = url;
			trace("Configration File:"+_url);
			_loader.load(new URLRequest(_url));
		}

//		/**
//		 * Get XML instance
//		 */
//		public function get xmlDoc():XML {
//			return _xmlDoc;
//		}

		/**
		 */
		public static function xmlDoc(url:String):XML {
			return _xmlDocs[url];
		}
		
		public static function set xml(value:XML):void {
			_xmlDocs[null] = value;
		}
		
		public static function attributeBoolean(xml:XML,name:String):Boolean {
			return xml.hasOwnProperty("@"+name)? xml.attribute(name).toString().toLowerCase() == "true" : false;
		}
		
		/**
		 */
		private function progressHandler(evt:ProgressEvent):void {
			trace("Reading...");
		}
		
		/**
		 */
		private function completeHandler(evt:Event):void {
			trace("Complete");
			try {
				_xmlDocs[_url] = new XML(_loader.data);
//				_xmlDoc = new XML(_loader.data);
				dispatchEvent(new Event(COMPLETED));
			} catch (err:Error) {
				trace("XML Error:" + err.message);
				trace(_loader.data);
			}
			removeAllListeners();
		}
		
		/**
		 */
		private function securityErrorHandler(evt:SecurityErrorEvent):void {
			//mx.controls.Alert.show("Security Error");
			trace("Security Error"+evt);
			removeAllListeners();
		}
		
		/**
		 */
		private function ioErrorHandler(evt:IOErrorEvent):void {
			//mx.controls.Alert.show("IO Error");
			trace("IO Error"+evt);
			removeAllListeners();
		}
		
		private function removeAllListeners():void {
			_loader.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
			_loader.removeEventListener(Event.COMPLETE,completeHandler);
			_loader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
			_loader.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
		}
	}
}